<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="文章标题">
                    <el-input
                        class="w-[280px]"
                        v-model="formData.name"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="分类名称">
                    <el-select class="w-[280px]" v-model="formData.class">
                        <el-option label="全部" value />
                        <el-option
                            v-for="item in optionsData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button type="primary"  @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <el-table ref="tableRef" class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" row-key="id">
                <el-table-column label="id" prop="id" min-width="100" />
                <el-table-column label="排序" prop="sort" min-width="100" />
                <el-table-column label="名称" prop="name" min-width="100" />
                <el-table-column label="图片" min-width="100">
                    <template #default="{ row }">
                        <img :src="row.img" width="100">
                    </template>
                </el-table-column>
                <el-table-column label="所属分类" prop="class_name" min-width="100" />
                <el-table-column label="状态" prop="is_display" min-width="100">
                    <template #default="{ row }">
                        <el-switch v-model="row.is_display" inline-prompt
                              width="100" :active-value="1"
                            :inactive-value="2" active-text="显" inactive-text="隐" @click="changeStatus(row)" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button type="danger" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                        <!-- <router-link  :to="{
                                    path: 'expand',
                                    query: {
                                        id: row.id,
                                    },
                                }"
                            >
                            <el-button type="primary" link>
                                拓展
                            </el-button>
                        </router-link> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { getChart,chartDelete,chartDis } from '@/api/application/website/swiper'
import { getClass,delClass } from '@/api/application/website/swiper'
import EditPopup from './add.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const formData = reactive({
    name:"",
    class:""
})
const showEdit = ref(false)
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}
const optionsData  =ref([

])
const getlist = async() => {
    const results = await getClass({})
    optionsData.value = results.lists
}
getlist()
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getChart,
    params: formData
})
const changeStatus = async (row: any) => {
    await chartDis({ is_display: row.is_display, id: row.id })
    getLists()
}
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await chartDelete({ id })
    getLists()
}
getLists()
</script>
